<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.Project"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.ProjectDAO"%>
<%@page import="db.Project"%>
<%@page import="db.TobDAO" %>
<%@page import="db.Tob" %>
<%@page import="db.TeamDAO" %>
<%@page import="db.Team" %>
<%@page import="db.PurreqDAO" %>
<%@page import="db.Purreq" %>
<%@page import="db.Supplier" %>
<%@page import="db.Place" %>
<%@page import="db.User"%>
<%@page import="db.UserDAO"%>
<%@page import="db.Group"%>
<%@page import="db.Purmat"%>
<%@page import="db.PurmatDAO"%>
<%@page import="db.Material"%>
<%@page import="db.MaterialDAO"%>
<%@page import="db.Outpurmat"%>
<%@page import="db.OutpurmatDAO"%>
<%@page import="db.Outreq"%>
<%@page import="db.OutreqDAO"%>
<%@page import="db.Receivepurmat"%>
<%@page import="db.ReceivepurmatDAO"%>
<%@page import="db.RetreqDAO"%>
<%@page import="db.ReceivepurmatDAO"%>
<%@page import="db.Retreq"%>
<%@page import="db.Retmat"%>
<%@page import="db.RetmatDAO"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	request.setCharacterEncoding("utf-8");
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	OutreqDAO outreqDAO = (OutreqDAO)ctx.getBean("OutreqDAO");
	OutpurmatDAO outpurmatDAO = (OutpurmatDAO)ctx.getBean("OutpurmatDAO");
	TobDAO tobDAO = (TobDAO)ctx.getBean("TobDAO");
	ProjectDAO projectDAO = (ProjectDAO)ctx.getBean("ProjectDAO");
	TeamDAO teamDAO = (TeamDAO)ctx.getBean("TeamDAO");
	ReceivepurmatDAO receivepurmatDAO = (ReceivepurmatDAO)ctx.getBean("ReceivepurmatDAO");
	UserDAO userDAO = (UserDAO)ctx.getBean("UserDAO");
	PurmatDAO purmatDAO = (PurmatDAO)ctx.getBean("PurmatDAO");
	RetreqDAO retreqDAO = (RetreqDAO)ctx.getBean("RetreqDAO");
	RetmatDAO retmatDAO = (RetmatDAO)ctx.getBean("RetmatDAO");
	String pid = request.getParameter("pid");
	
	Retreq retreq = new Retreq();
	Tob tob = new Tob();
	Team team = new Team();
	Project project = new Project();
	Supplier supplier = new Supplier();
	User requser = new User();
	User mauser = new User();
	Material material = new Material();
	List retmatlist = new ArrayList();
	Outreq outreq= new Outreq();
	if(pid!=null){
		retreq = retreqDAO.findById(Integer.parseInt(pid));
		retmatlist = retmatDAO.findByRetmatIdAndStateDel(Integer.parseInt(pid),1);
	}
	String tobName = "";
	
	if(retreq!=null){//查询业务类型
		int tid = 0;
		tid = retreq.getTobId();
		outreq= retreq.getOutreq();
		project =  outreq.getProject();
		tob = tobDAO.findById(tid);
		if(tob!=null){
			tobName = tob.getName();
		}else{
			tobName = "";
		}
		
		int teamid = 0;
		teamid = retreq.getTeamId();
		int suppid = 0;
		team = teamDAO.findById(teamid);
		int requid = 0;
		if(retreq.getReqUserId()!=null){
			requid = retreq.getReqUserId();
			requser = userDAO.findById(requid);
		}
		int mauid = 0;
		if(retreq.getMaUserId()!=null){
			mauid = retreq.getMaUserId();
		}
		
		if(mauid!=0){
			mauser = userDAO.findById(mauid);
		}
		
		
	}
	Group reqGroup = new Group();
	Group assGroup = new Group();
	if(requser!=null){
		reqGroup = requser.getGroup();
	}
	if(mauser!=null){
		assGroup =mauser.getGroup();
	}
	boolean senpi = false;
	int userid = (Integer)session.getAttribute("id");
	if(retreq.getMaUserId()!=null && retreq.getMaUserId()==userid){
		senpi=true;
	}else if(retreq.getMaUserId()==null){
		senpi=true;
	}
	request.setAttribute("purmatlist",getListReMap2(retmatlist,receivepurmatDAO));
	request.setAttribute("reqGroup",reqGroup);
	request.setAttribute("requser",requser);
	request.setAttribute("assGroup",assGroup);
	request.setAttribute("mauser",mauser);
	request.setAttribute("supplier",supplier);
	request.setAttribute("team",team);
	request.setAttribute("project",project);
	request.setAttribute("tobName",tobName);
	request.setAttribute("outreq",outreq);
	request.setAttribute("retreq",retreq);
	request.setAttribute("senpi",senpi);
	request.setAttribute("pid",pid);
	
	
	
	
	pageContext.forward("/vm/returnStock/retApp_1_1.vm");
%>
<%!

	public List getListReMap2(List list,ReceivepurmatDAO receivepurmatDAO){
		List listmap = new ArrayList();
		if(list != null && list.size()>0){
			for(int i=0;i<list.size();i++){
				Map map = new HashMap();
				Retmat retmat = new Retmat();
				retmat = (Retmat)list.get(i);
				if(retmat!=null){
					map.put("retpid", retmat.getId());//
					Outpurmat outpurmat = retmat.getOutpurmat();
					map.put("retnum", retmat.getRetNum());//申退数量
					map.put("outnum", outpurmat.getOutReqNum());//出库数量
					map.put("retreqnum", outpurmat.getRetNum());//退库总数量
					map.put("useretnum", retmat.getRetNum());//可退库数量
					map.put("taskcode", outpurmat.getTaskCode());
					//
					//获取总库存表相应数据
					//Receivepurmat receivepurmat = outpurmat.getReceivepurmat();
					
					
					Receivepurmat receivepurmat =outpurmat.getReceivepurmat();// receivepurmatDAO.findById(outpurmat.getReceivePurmatId());
					//System.out.println("test=====================>"+outpurmat.getReceivePurmatId());
					if(receivepurmat!=null){
					map.put("purprice",receivepurmat.getPurPrice());//申购价格
					map.put("patch",receivepurmat.getBatch());//申购价格
					Place place = receivepurmat.getPlace();
						if(place!=null){
							map.put("place", place.getName());
						}else{
							map.put("place","");
						}
						if(receivepurmat.getMaterial()!=null){
							Material material = receivepurmat.getMaterial();
							map.put("matcode", material.getCode());
							map.put("matname", material.getName());
							map.put("matcom", material.getCompany());
						}
					}
						
					
					
				}
				listmap.add(map);
			}
		}
		return listmap;
	}
%>